package com.jic.market.impl;

import com.google.common.collect.Lists;
import com.jic.market.common.constant.PatchGrouponInstConstant;
import com.jic.market.common.constant.PatchGrouponInstDetailConstant;
import com.jic.market.entity.PatchGrouponDetailEntity;
import com.jic.market.mapper.PatchGrouponDetailEntityMapper;
import com.jic.market.orm.Pagination;
import com.jic.market.request.PatchGrouponInstDetailRequest;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PatchGrouponInstDetailResponse;
import com.jic.market.response.PatchGrouponInstResponse;
import com.jic.market.service.PatchGrouponInstDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author guohuayong
 * @date: 2020年01月21日
 */
@Service
@Slf4j
public class PatchGrouponInstDetailServiceImpl implements PatchGrouponInstDetailService {
    @Resource
    private PatchGrouponDetailEntityMapper curMapper;

    @Override
    public PageResponse<PatchGrouponInstDetailResponse> query(PatchGrouponInstDetailRequest request) {
        Pagination pagination = Pagination.build(request.getStart(), request.getLimit(), request.genOrderBys());

        List<PatchGrouponInstDetailResponse> list = curMapper.list(request, pagination);
        int count = curMapper.listCount(request);

        list = Optional.ofNullable(list).orElse(Lists.newArrayList()).stream().map(e->{
            e.setStatusName(PatchGrouponInstDetailConstant.StatusEnum.getRemarkByCode(e.getStatus()));
            e.setPaymentTypeName(PatchGrouponInstDetailConstant.PaymentTypeEnum.getRemarkByCode(e.getPaymentType()));
            e.setPaymentStatusName(PatchGrouponInstDetailConstant.PaymentStatusEnum.getRemarkByCode(e.getPaymentStatus()));
            return e;
        }).collect(Collectors.toList());


        PageResponse<PatchGrouponInstDetailResponse> responsePageResponse = new PageResponse<PatchGrouponInstDetailResponse>()
                .setPageNo(request.getPageNo())
                .setPageSize(request.getPageSize())
                .setTotal(count)
                .setRows(list);

        return responsePageResponse;
    }

    @Override
    public PatchGrouponDetailEntity queryEntity(PatchGrouponDetailEntity detailEntityCondition) {
        PatchGrouponDetailEntity detailEntity = curMapper.query(detailEntityCondition);
        return detailEntity;
    }

    @Override
    public List<PatchGrouponDetailEntity> findAll(PatchGrouponDetailEntity detailEntityCondition) {
        List<PatchGrouponDetailEntity> detailEntityList = curMapper.findAll(detailEntityCondition);
        return detailEntityList;
    }

    @Override
    public Integer addDetailEntity(PatchGrouponDetailEntity detailEntity) {
        int count = curMapper.insert(detailEntity);
        return count;
    }

    @Override
    public Integer updateEntity(PatchGrouponDetailEntity detailEntity) {
        int count = curMapper.update(detailEntity);
        return count;
    }

}
